package com.ssyc.gsk_teacher.adapter;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.makeramen.roundedimageview.RoundedImageView;
import com.ssyc.common.glide.ImageUtil;
import com.ssyc.common.utils.ScreenUtils;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.gsk_teacher.R;
import com.ssyc.gsk_teacher.bean.BirthdayInfo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 生日提醒的适配器
 * Created by Administrator on 2018/7/4 0004.
 */

public class BirthdayGvAdapter extends BaseQuickAdapter<BirthdayInfo.ListBean, BaseViewHolder> {
    private Context context;
    private LinearLayout.LayoutParams lp;
    private Handler handler;

    public BirthdayGvAdapter(Context context, int layoutResId, @Nullable List<BirthdayInfo.ListBean> data) {
        super(layoutResId, data);
        this.context = context;
        handler = new Handler();
        //获取屏幕宽度
        int screenWidth = ScreenUtils.getScreenWidth(context);
        //计算条目的最外层的宽度
        int itemWidth = ((screenWidth - UiUtils.dp2px(16 * 2)) / 3);
        lp = new LinearLayout.LayoutParams(itemWidth, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    protected void convert(BaseViewHolder helper, BirthdayInfo.ListBean item) {
        //条目的外城布局
        RelativeLayout rlOut = helper.getView(R.id.rl_out);
        rlOut.setLayoutParams(lp);
        //头像
        RoundedImageView ivHead = helper.getView(R.id.iv_head);
        ImageUtil.displayImage(context, ivHead, item.getIcon());
        //姓名
        TextView tvName = helper.getView(R.id.tv_name);
        tvName.setText(item.getName());
        //选中的标签
        ImageView ivState = helper.getView(R.id.iv_state);
        if (item.isSelected()) {
            //选中
            ivState.setImageResource(R.drawable.teacher_birthday_selected);
        } else {
            //非选中
            ivState.setImageResource(R.drawable.teacher_birthday_normal);
        }

        //整个条目的点击事件
        RelativeLayout rl_conent = helper.getView(R.id.rl_conent);
        rl_conent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                item.setSelected(!item.isSelected());
                notifyRefresh(helper.getLayoutPosition());
            }
        });

        //时间,服务器返回2014-07-02 00:00:00.0,格式化时间
        TextView tvTime = helper.getView(R.id.tv_time);
        tvTime.setText(getTime(item.getBirthday()));

        Log.i("test","tvTime:"+item.getBirthday());
    }

    /**
     * 根据服务器返回时间获取年月的方法
     *
     * @return
     */
    private String getTime(String stringDate) {
        String res = "";
        try {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sf.parse(stringDate);
            //在将date转化为年月
            SimpleDateFormat s = new SimpleDateFormat("MM-dd");
            res = s.format(date);
            res = res.replace("-","月")+"号";
        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;
    }

    /**
     * 在item点击事件中直接使用notifyDataSetChanged()
     * 解决java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling
     *
     * @param pos
     */
    private void notifyRefresh(final int pos) {
        Runnable r = new Runnable() {
            public void run() {
                notifyItemChanged(pos);
            }
        };
        handler.post(r);
    }

    /**
     * 提供给Activity释放handler对象的方法,防止内存泄漏
     */
    public void releaseHandler() {
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
    }
}
